﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MinoChat.Models;
using MinoChat.Services;
using MinoChat.ViewModels;
using MinoChat.ViewModels.Pages;
using MinoChat.Views;
using MinoChat.Views.Pages;
using Serilog;
using System.Windows;
using MinoChat.Services.Interfaces;
using MinoChat.Data.Configuration;
using MinoChat.Data.Services;
using MinoChat.Data.Services.Interfaces;
using MinoChat.Provider.Services;
using MinoChat.Provider.Services.Interfaces;

namespace MinoChat;

public partial class App : Application
{
    private readonly IHost _host;
    private readonly ILogger<App> _logger;
    //外部可以获取容器App.Services.GetRequiredService<T>
    public App()
    {
        //默认就会注入IServicsProvide，你可以在任何地方获取来解析其他服务
        _host = CreateHostBuilder().Build();
        _logger = _host.Services.GetRequiredService<ILogger<App>>();
    }
    private static HostApplicationBuilder CreateHostBuilder()
    {
        var builder = Host.CreateApplicationBuilder();
        // 【1】配置程序设置config
        builder.Configuration.Sources.Clear();
        builder.Configuration
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
        // 启用Options模式
        builder.Services.Configure<AppSettings>(
            builder.Configuration.GetSection("AppSettings"));
        // 【2】配置日志Serilog
        builder.Logging.ClearProviders();
        builder.Logging.AddSerilog(new LoggerConfiguration()
            .WriteTo.File("Logs/MinoChat.log", rollingInterval: RollingInterval.Day).WriteTo.Console()
            .CreateLogger());
        // 【3】后台服务初始化主窗口并导航到首页
        builder.Services.AddHostedService<ApplicationHostService>();

        // 【4】注册FreeSql和数据层服务
        builder.Services.AddSingleton<IFreeSql>(sp =>
            FreeSqlConfig.CreateInstance(sp.GetRequiredService<ILogger<IFreeSql>>())
        );
        // 注册数据层服务
        builder.Services.AddSingleton<IChatDataService, ChatDataService>();
        builder.Services.AddSingleton<IChatSearchService, ChatSearchService>();
        builder.Services.AddSingleton<IChatStatisticsService, ChatStatisticsService>();
        builder.Services.AddSingleton<IBackupService, BackupService>();

        // 【4.5】注册 Provider 服务
        builder.Services.AddSingleton<IProviderConfigService, ProviderConfigService>();
        builder.Services.AddSingleton<IProviderService, ProviderService>();
        builder.Services.AddSingleton<ITitleSummaryService, TitleSummaryService>();

        // 【5】注册其他服务
        builder.Services.AddSingleton<INavigationService, NavigationService>();
        builder.Services.AddSingleton<IConfigService, ConfigService>();
        builder.Services.AddSingleton<SystemThemeWatcher>();
        builder.Services.AddSingleton<IThemeService, ThemeService>();
        builder.Services.AddSingleton<IUserInputService, UserInputService>();
        builder.Services.AddSingleton<MinoChat.Ui.Services.SnackbarService>();
        //注册页面
        builder.Services.AddSingleton<ChatPage>();
        builder.Services.AddSingleton<MaskPage>();
        builder.Services.AddSingleton<SettingsPage>();
        builder.Services.AddSingleton<SearchPage>();
        // 注册视图模型
        builder.Services.AddSingleton<ChatPageViewModel>();
        builder.Services.AddSingleton<MaskPageViewModel>();
        builder.Services.AddSingleton<SettingsPageViewModel>();
        builder.Services.AddSingleton<SearchPageViewModel>();
        builder.Services.AddSingleton<MainViewModel>();
        // 注册主窗口
        builder.Services.AddSingleton<MainWindow>(sp => new MainWindow(
            sp.GetRequiredService<MainViewModel>(),
            sp.GetRequiredService<INavigationService>(),
            sp.GetRequiredService<IThemeService>(),
            sp.GetRequiredService<MinoChat.Ui.Services.SnackbarService>()));
        return builder;
    }
    protected override void OnStartup(StartupEventArgs e)
    {
        _host.Start();
        base.OnStartup(e);
        _logger.LogInformation("应用程序启动!");
    }
    protected override void OnExit(ExitEventArgs e)
    {
        _logger.LogInformation("应用程序关闭!");
        _host.Dispose();
        base.OnExit(e);
    }
}